package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.OptimalShop;
import com.addplus.townmall.server.api.model.town.expand.OptimalShopExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商城优选商品 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface OptimalShopMapper extends BaseAddMapper<OptimalShop> {

    @Select("select s.store_name,sp.sale_virtual_count,spd.shop_comment_count,os.* " +
            "from optimal_shop  os " +
            "inner JOIN store  s ON s.id = os.store_id " +
            "inner JOIN shop_product sp ON os.shop_product_id = sp.id "+
            "inner JOIN shop_product_describe spd ON sp.id = spd.shop_product_id " +
            "where ${ew.sqlSegment}")
    List<OptimalShopExpand> getOptimalShopByQry(Page page, @Param("ew") Wrapper wrapper);


    @Select("<script>\n" +
            "SELECT\n" +
            " p.id,\n" +
            " p.store_id,\n" +
            " p.shop_product_id,\n" +
            " p.shop_sku_id,\n" +
            " p.shop_name,\n" +
            " p.shop_describe,\n" +
            " p.shop_seal_img,\n" +
            " p.sale_price,\n" +
            " p.line_price,\n" +
            " s.id AS store_id,\n" +
            " s.store_name,\n" +
            " s.icon_path,\n" +
            " d.good_rate,\n" +
            " d.total_comment\n" +
            "FROM\n" +
            " optimal_shop p\n" +
            " LEFT JOIN store s ON s.id = p.store_id\n" +
            " AND s.store_status = 1\n" +
            " AND s.is_delete = 0\n" +
            " LEFT JOIN shop_product sp ON s.id = p.shop_product_id\n" +
            " AND sp.is_deleted  = 0\n" +
            " LEFT JOIN shop_comment_all d ON d.shop_product_id = p.shop_product_id\n" +
            " AND d.is_deleted = 0\n" +
            " LEFT JOIN shop_product_describe sd ON sd.shop_product_id = p.shop_product_id\n" +
            " AND sd.is_deleted = 0\n" +
            "WHERE\n" +
            " p.is_delete = 0\n" +
            " AND p.online_status = 1\n" +
            "<if test=\"overallRateSort != null or saleSort != null or serviceRateSort != null or priceSort != null \">\n" +
            "ORDER BY\n" +
            "</if> \n" +
            "    <if test=\"overallRateSort != null \">\n" +
            "        sd.shop_product_overall_rate \n" +
            "        <if test=\"overallRateSort == 0\">\n" +
            "            ASC \n" +
            "        </if>\n" +
            "        <if test=\"overallRateSort == 1\">\n" +
            "            DESC \n" +
            "        </if>\n" +
            "        <if test=\"saleSort != null or serviceRateSort != null or priceSort != null \">\n" +
            "            ,\n" +
            "        </if>\n" +
            "    </if>\n" +
            "    <if test=\"saleSort != null \">\n" +
            "        sp.sale_count \n" +
            "        <if test=\"saleSort == 0\">\n" +
            "            ASC\n" +
            "        </if>\n" +
            "        <if test=\"saleSort == 1\">\n" +
            "            DESC\n" +
            "        </if>\n" +
            "        <if test=\"serviceRateSort != null or priceSort != null \">\n" +
            "            ,\n" +
            "        </if>\n" +
            "    </if>\n" +
            "    <if test=\"serviceRateSort != null \">\n" +
            "        sd.shop_product_service_rate \n" +
            "        <if test=\"serviceRateSort == 0\">\n" +
            "            ASC\n" +
            "        </if>\n" +
            "        <if test=\"serviceRateSort == 1\">\n" +
            "            DESC\n" +
            "        </if>\n" +
            "        <if test=\"priceSort != null \">\n" +
            "            ,\n" +
            "        </if>\n" +
            "    </if>\n" +
            "    <if test=\"priceSort != null \">\n" +
            "         sp.sale_price \n" +
            "        <if test=\"priceSort == 0\">\n" +
            "            ASC\n" +
            "        </if>\n" +
            "        <if test=\"priceSort == 1\">\n" +
            "            DESC\n" +
            "        </if>\n" +
            "    </if>\n" +
            "</script>")
    List<OptimalShopExpand> getOptimalShopExpandList(Page page, @Param("overallRateSort") Integer overallRateSort,
                                                     @Param("saleSort") Integer saleSort,
                                                     @Param("serviceRateSort") Integer serviceRateSort,
                                                     @Param("priceSort") Integer priceSort);

    @Select("select s.store_name,os.* " +
            "from optimal_shop  os " +
            "Left JOIN store  s ON s.id = os.store_id where os.is_delete=0 order by os.gmt_modified desc")
    List<OptimalShopExpand> getAllOptimalShop(IPage page);

    @Select("select s.store_name,sp.shop_tag,sku.sku_describe,sku.sale_price as sku_sale_price,sku.line_price as sku_line_price,sku.stock,sku.limit_num,os.* " +
            "from optimal_shop  os " +
            "Left JOIN store  s ON s.id = os.store_id Left JOIN shop_product sp ON os.shop_product_id = sp.id Left JOIN shop_specification_sku sku ON os.shop_sku_id = sku.id where os.is_delete=0 and os.online_status=1 and os.shop_product_id=#{shopId}")
    List<OptimalShopExpand> getOptimalShopByShopId(@Param("shopId") Integer shopId);

    @Select("select sku.sku_describe,sku.sale_price as sku_sale_price,sku.line_price as sku_line_price,sku.stock,sku.limit_num,os.shop_sku_id " +
            "from optimal_shop  os " +
            "Left JOIN shop_specification_sku sku ON os.shop_sku_id = sku.id where os.is_delete=0 and os.shop_product_id=#{shopId}")
    List<OptimalShopExpand> getSkuList(@Param("shopId") Integer shopId);

    @Select("select * from optimal_shop os where os.shop_product_id =#{shopId}")
    List<OptimalShopExpand> changeShopStatusById(@Param("shopId") Integer id);

}
